﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Configuration;
using System.Web.Services;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // ClientScript.RegisterStartupScript(this.GetType(), "load page", "<script>mapHaNoi();</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "load page", "<script>setActiveShopMap('"+ 3+ "');</script>");
    }
    /// <summary>
    /// 
    /// </summary>
    [WebMethod()]
    public static ShopMap getShopMapFromDataBaseId(string id)
    {
        ShopMap shop = null;
        try
        {
            shop = new ShopMap();
            SqlConnection conn = OpenConnection();
            SqlDataAdapter da = new SqlDataAdapter("Select * from tbShops Where id =" + id, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                shop.Id = dr["id"].ToString();
                shop.Name = dr["name"].ToString();
                shop.Description = dr["description"].ToString();
                shop.Address = dr["address"].ToString();
                shop.Tell = dr["tel"].ToString();
                shop.Phone = dr["phone"].ToString();
                shop.Image = dr["image"].ToString();
                shop.X = dr["x"].ToString();
                shop.Y = dr["y"].ToString();
            }
            CloseConnection(conn);
            return shop;
        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
            return null;
        }
    }
    /// <summary>
    /// Phuong thuc tra ve ShopMap co id lon nhat duoc goi luc nguoi dung them diem
    /// </summary>
    /// <returns></returns>
    [WebMethod()]
    public static ShopMap getShopMapHaveIdMax()
    {
        ShopMap shop = null;
        try
        {
            shop = new ShopMap();
            SqlConnection conn = OpenConnection();
            SqlDataAdapter da = new SqlDataAdapter("Select * from tbShops Where id = (Select Max(id) from tbShops)", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                shop.Id = dr["id"].ToString();
                shop.Name = dr["name"].ToString();
                shop.Description = dr["description"].ToString();
                shop.Address = dr["address"].ToString();
                shop.Tell = dr["tel"].ToString();
                shop.Phone = dr["phone"].ToString();
                shop.Image = dr["image"].ToString();
                shop.X = dr["x"].ToString();
                shop.Y = dr["y"].ToString();
            }
            CloseConnection(conn);
            return shop;
        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
            return null;
        }

    }
    /// <summary>
    /// Phuong thuc luu thong tin cua mot diem sau khi keo
    /// Sau khi keo thi luu thong tin vi tri cua no,truyen vao id va vi tri luu
    /// </summary>
    /// <param name="id"></param>
    /// <param name="x"></param>
    /// <param name="y"></param>
    [WebMethod()]
    public static void saveShopAffterMove(int id, string x, string y)
    {
        try
        {
            string updateSql = "UPDATE tbShops " + "SET x = @x,y = @y" + " WHERE id = @id";
            SqlConnection conn = OpenConnection();
            SqlCommand UpdateCmd = new SqlCommand(updateSql, conn);

            UpdateCmd.Parameters.Add("@id", SqlDbType.Int, 10, "id");
            UpdateCmd.Parameters.Add("@x",SqlDbType.Text,30,"x");
            UpdateCmd.Parameters.Add("@y", SqlDbType.Text, 30, "y");


            UpdateCmd.Parameters["@id"].Value = id;
            UpdateCmd.Parameters["@x"].Value = x;
            UpdateCmd.Parameters["@y"].Value = y;

            UpdateCmd.ExecuteNonQuery();
            CloseConnection(conn);
        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }
    }
    /// <summary>
    /// Lay 1 list Shopmap de truyen sang ben kia de ve
    /// ShopMap la 1 doi tuong chua day du nhung thong tin cua 1 shop
    /// </summary>
    /// <returns>List<ShopMap></returns>
    [WebMethod()]
    public static List<ShopMap> getListShopMapFromDataBase()
    {
        List<ShopMap> list = null;
        try
        {
            list = new List<ShopMap>();
            SqlConnection conn = OpenConnection();
            SqlDataAdapter da = new SqlDataAdapter("Select * from tbShops", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ShopMap a = new ShopMap();
                a.id = dr["id"].ToString();
                a.Name = dr["name"].ToString();
                a.Description = dr["description"].ToString();
                a.Address = dr["address"].ToString();
                a.Tell = dr["tel"].ToString();
                a.Phone = dr["phone"].ToString();
                a.Image = dr["image"].ToString();
                a.X =  dr["x"].ToString();
                a.Y = dr["y"].ToString();
                //add shop vao list
                list.Add(a);
            }
            CloseConnection(conn);
            return list;
        }
        catch (System.Exception ex)
        {
            return null;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="place_id"></param>
    /// <returns></returns>
    [WebMethod()]
    public static List<ShopMap> getListShopMapFromDataBase(int place_id)
    {
        List<ShopMap> list = null;
        try
        {
            list = new List<ShopMap>();
            SqlConnection conn = OpenConnection();
            SqlDataAdapter da = new SqlDataAdapter("Select * from tbShops Where place_id =" + place_id + " And del_flag = 0", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ShopMap a = new ShopMap();
                a.id = dr["id"].ToString();
                a.Name = dr["name"].ToString();
                a.Description = dr["description"].ToString();
                a.Address = dr["address"].ToString();
                a.Tell = dr["tel"].ToString();
                a.Phone = dr["phone"].ToString();
                a.Image = dr["image"].ToString();
                a.X = dr["x"].ToString();
                a.Y = dr["y"].ToString();
                //add shop vao list
                list.Add(a);
            }
            CloseConnection(conn);
            return list;
        }
        catch (System.Exception ex)
        {
            return null;
        }
    }
    /// <summary>
    /// ham dem so shop dang hoat dong
    /// </summary>
    /// <param name="place_id"></param>
    /// <returns></returns>
    [WebMethod()]
    public static int[] getCountRowPlaceId(int sum_place_id)
    {
        System.Diagnostics.Debug.WriteLine("Vao ham");
        int[] active = new int[sum_place_id];
        List<string> listSql = new List<string>();
        for (int i = 0; i < sum_place_id; i++)
        {
            int j = i;
            j++;
            string sql = "SELECT COUNT(del_flag) FROM  tbShops WHERE (del_flag = 0 And place_id = " + j + ")";
            listSql.Add(sql);
        }        
        try
        {
            SqlConnection conn = OpenConnection();
            for (int j = 0; j < sum_place_id; j++)
            {
                try
                {
                    SqlCommand cmd = new SqlCommand(listSql[j], conn);
                    int count = (Int32)cmd.ExecuteScalar();
                    active[j] = count;
                }
                catch (System.Exception ex)
                {
                    active[j] = 0;
                }               
            }
            CloseConnection(conn);
            return active;
        }
        catch (System.Exception e)
        {
            return active;
        }
    }
    /// <summary>
    /// 
    /// Ham nay tra ve gia tri cac shop dang hoat dong hoac khong hoat dong
    /// </summary>
    /// <returns></returns>
    [WebMethod()]
    public static int[] getCountActivePlaceId(int _id_place_active)
    {
        string sql = "SELECT COUNT(del_flag) FROM  tbShops WHERE (del_flag = 0 And place_id = " + _id_place_active + ")";
        string sql1 = "SELECT COUNT(del_flag) FROM  tbShops WHERE (del_flag = 1 And place_id = " + _id_place_active + ")";
        int[] ketqua = new int[3];
        SqlConnection conn = OpenConnection();
        try
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            int count = (Int32)cmd.ExecuteScalar();
            ketqua[0] = count;
        }
        catch (System.Exception ex)
        {
            ketqua[0] = 0;
        }
        try
        {
            SqlCommand cmd = new SqlCommand(sql1, conn);
            int count = (Int32)cmd.ExecuteScalar();
            ketqua[1] = count;
        }
        catch (System.Exception ex)
        {
            ketqua[1] = 0;
        }
        finally
        {
            ketqua[2] = ketqua[0] + ketqua[1];
        }
        CloseConnection(conn);
        return ketqua;
    }
    [WebMethod()]
    public static SqlConnection OpenConnection()
    {
        try
        {
            ConnectionStringSettings settings = WebConfigurationManager.ConnectionStrings["shop_ConnectionString"];
            SqlConnection connection = new SqlConnection(settings.ConnectionString);
            connection.Open();
            return connection;
        }
        catch (Exception)
        {
            return null;
        }
    }
    /// <summary>
    /// Đóng kết nối đến cơ sở dữ liệu
    /// </summary>
    /// <param name="connection"></param>
    [WebMethod()]
    public static void CloseConnection(SqlConnection connection)
    {
        if (connection != null)
        {
            connection.Close();
        }
    }
}
